package com.example.hsh.charbarpopupwindow;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.appcompat.AlertDialogPositiveButtonHack;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    protected ArrayList<String> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) this.findViewById(R.id.testBtn);
//        initData();
//        final ListViewAdapter adapter = new ListViewAdapter(this);
//        adapter.addDatas(datas);
//        final ListPopupWindow popupWindow = new ListPopupWindow(this);
//        popupWindow.setAdapter(adapter);
//
//        popupWindow.setAnchorView(btn);
//        popupWindow.setHeight(600);
//        popupWindow.setWidth(300);
//        popupWindow.setVerticalOffset(-100);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                System.out.println("test btn");
//                popupWindow.show();
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//
//                        popupWindow.setHeight(1000);
////                        popupWindow.setVerticalOffset(-100);
////                        adapter.notifyDataSetChanged();
//                        adapter.addDatas(datas);
//                        popupWindow.show();
//                    }
//                },1000);

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                AlertDialog dialog = builder.setTitle("test")
                        .setMessage("test hack")
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
//                AlertDialogPositiveButtonHack.hackPositiveButton(dialog, "确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MainActivity.this, "hack success", Toast.LENGTH_SHORT).show();
//                    }
//                });
                Window mWindow = dialog.getWindow();

                View parentPanel = mWindow.findViewById(android.support.v7.appcompat.R.id.parentPanel);
                View defaultButtonPanel = parentPanel.findViewById(android.support.v7.appcompat.R.id.buttonPanel);

                Button mButtonPositive = (Button) defaultButtonPanel.findViewById(android.R.id.button1);

                if (mButtonPositive != null) {
                    mButtonPositive.setText("确定");
                    mButtonPositive.setVisibility(View.VISIBLE);
                    mButtonPositive.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this, "hack success", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

    }

    private void initData() {
        for (int i=0;i<50;i++) {
            datas.add("test " + i);
        }
    }
}
